package com.habbot.beautifyinghandbook.daos;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.habbot.beautifyinghandbook.helpers.AppConfig;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter extends SQLiteOpenHelper {

	public static String DB_PATH = "";

	private SQLiteDatabase dataBase;
	private final Context context;
	private static DBAdapter connection;
	public static final String TT = "";

	private DBAdapter(Context context) {
		super(context, AppConfig.DB_NAME, null, 1);
		this.context = context;
		DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
		try {
			createDataBase();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

	}

	public static synchronized DBAdapter getInstance(Context context) {
		if (connection == null) {
			connection = new DBAdapter(context);
		}
		return connection;
	}

	public void createDataBase() throws IOException {
		if (!checkDataBase()) {
			this.getReadableDatabase();
			try {
				copyDataBase();
				Log.d("xyz", "copied DB");
			} catch (IOException e) {
				Log.d("xyz", "copied DB error");
				throw new Error("Error copying database");
			}
		}
	}

	private boolean checkDataBase() {
		try {
			String myPath = DB_PATH + AppConfig.DB_NAME;
			File f = new File(myPath);
			if (!f.exists())
				return false;
			SharedPreferences sharedPreferences = context.getSharedPreferences(
					AppConfig.SHARE_PREFERENCE, Context.MODE_PRIVATE);
			String preVersion = sharedPreferences.getString(
					AppConfig.EXTRA_PREVIOUS_VESION, null);
			String version = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionName;
			Log.d("VERSION", preVersion + "   :   " + version);
			if (preVersion == null || (!version.equalsIgnoreCase(preVersion)))
				return false;
			else
				return true;

		} catch (SQLiteException e) {
			e.printStackTrace();
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	private void copyDataBase() throws IOException {
		String[] dbFiles = context.getAssets().list(AppConfig.ASSETS_DB_FOLDER);
		String outFileName = DB_PATH + AppConfig.DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		for (int i = 0; i < dbFiles.length; i++) {

			InputStream myInput = context.getAssets().open(
					AppConfig.ASSETS_DB_FOLDER + "/" + dbFiles[i]);
			byte[] buffer = new byte[1024];
			int length;
			while ((length = myInput.read(buffer)) > 0) {
				myOutput.write(buffer, 0, length);
			}
			myInput.close();
		}
		myOutput.flush();
		myOutput.close();
	}

	public SQLiteDatabase openDataBase() throws SQLException {
		String myPath = DB_PATH + AppConfig.DB_NAME;
		dataBase = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READWRITE);
		return dataBase;
	}

	@Override
	public synchronized void close() {
		if (dataBase != null)
			dataBase.close();
		super.close();
	}

}
